package com.wb.base.refreshview;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public abstract class LoadRecyclerViewAdapter<T> extends IRecyclerAdapter<T, RecyclerView.ViewHolder> {

    private Context context;
    public LayoutInflater factory;

    public Context getContext() {
        return context;
    }

    public LoadRecyclerViewAdapter(OnItemCLickListener listener) {
        super(listener);
    }


    public class VIEW_TYPES {
        public static final int NORMAL = 0;
        public static final int HEADER = 1;
        public static final int FOOTER = 2;
        public static final int CHANGED_FOOTER = 3;
    }

    //加载更多显示
    private boolean isLoadMoreIsShow = true;

    private View loadView;

    public BaseViewHolder onLoadViewHolder(ViewGroup parent, int viewType) {
        return null;
    }


    protected abstract int contentView(int viewType);

    public void onLoadHolder(RecyclerView.ViewHolder holder, int position) {
    }

    public int getViewType(int position) {
        return VIEW_TYPES.NORMAL;
    }

    public void setIsLoadMoreIsShow(boolean isLoadMoreIsShow) {
        if (this.isLoadMoreIsShow != isLoadMoreIsShow) {
            if (loadView != null && this.isLoadMoreIsShow) {
                this.isLoadMoreIsShow = isLoadMoreIsShow;
                notifyItemRemoved(getItemCount() - 1);
            } else if (loadView != null) {
                this.isLoadMoreIsShow = isLoadMoreIsShow;
                notifyItemInserted(getItemCount());
            }
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int type = getItemViewType(position);
        if (type == VIEW_TYPES.FOOTER || type == VIEW_TYPES.CHANGED_FOOTER) {
            onLoadHolder(holder, position);
        } else {
            if (onItemCLickListener != null)
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onItemCLickListener != null)
                            onItemCLickListener.onItemClick(position, v);
                    }
                });
            onBindHolder((BaseViewHolder) holder, position);
        }
    }


    public abstract void onBindHolder(BaseViewHolder holder, int position);

    @Override
    public final int getItemViewType(int position) {
        if (position == getCount() && loadView != null && isLoadMoreIsShow) {
            if (isLoadMoreIsShow) {
                return VIEW_TYPES.CHANGED_FOOTER;
            } else {
                return VIEW_TYPES.FOOTER;
            }
        } else {
            return getViewType(position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (context == null) {
            context = parent.getContext();
            this.factory = LayoutInflater.from(context);
        }

        if (viewType == VIEW_TYPES.FOOTER || viewType == VIEW_TYPES.CHANGED_FOOTER) {
            RecyclerView.ViewHolder viewHolder = onLoadViewHolder(parent, viewType);

            if (viewHolder == null) {
                viewHolder = new DefaultLoadHolder(loadView);
                return viewHolder;
            }
            return viewHolder;
        } else {
            BaseViewHolder viewHolder = new BaseViewHolder(factory.inflate(contentView(viewType), parent, false));
            return viewHolder;
        }
    }


    @Override
    public final int getItemCount() {
        int size = getCount();
        if (size == 0) {
            return 0;
        }
        if (loadView != null && isLoadMoreIsShow) {
            return size + 1;
        } else {
            return size;
        }

    }

    public View getLoadMoreView() {
        return loadView;
    }

    public void setLoadMoreView(View loadView) {
        this.loadView = loadView;
    }

    class DefaultLoadHolder extends RecyclerView.ViewHolder {
        public DefaultLoadHolder(View itemView) {
            super(itemView);
        }
    }

}
